package ui;

import info.Manuscript;
import info.Review;
import io.ManuscriptAccess;
import io.ReviewAccess;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTable;

import users.Reviewer;

/**
 * The primary panel that the Reviewer will use to carry out his or her duties.
 * 
 * @author Aaron Chen
 * @version 3/20/2014
 */
@SuppressWarnings("serial")
public class ReviewerPanel extends AbstractMainComponent {
  
  /** The String to show if there is nothing in that particular cell. */
  private static final String STRING_NULL = "-";

  /** The index value of the manuscript author column. */
  private static final int COLUMN_AUTHOR_INDEX = 0;
  /** The index value of the manuscript title column. */
  private static final int COLUMN_TITLE_INDEX = 1;
  /** The index value of the manuscript abstract column. */
  private static final int COLUMN_ABSTRACT_INDEX = 2;
  /** The index value of the reviewer's review score for the manuscript column. */
  private static final int COLUMN_SCORE_INDEX = 3;
  /** The index value of the manuscript viewing button column. */
  private static final int COLUMN_BUTTON_INDEX = 4;
  
  /** The header labels. */
  private static final String[] HEADERS = {
    "Author",
    "Title",
    "Abstract",
    "Review Score",
    "" //view conference details button
  };

  /** The columns that will be marked as editable and thus have buttons. */
  private static final boolean[] BOOLS = {false, true, false, false, true};
  
  /** The reviewer that is using this panel. */
  private Reviewer my_reviewer;
  

  /**
   * Constructs a new Reviewer panel for the Reviewer to use.
   * @param the_stack    The main application window container.
   * @param the_reviewer The Reviewer using this panel.
   */
  public ReviewerPanel(final DisplayPanel the_stack, final Reviewer the_reviewer) {
    super(the_stack);
    setFrameParameters();
    my_reviewer = the_reviewer;
    final JPanel main_panel = createMainPanel();
    add(main_panel);
  }
  
  
  /**
   * Create the main panel.
   * @return The main panel.
   */
  private JPanel createMainPanel() {
    final JPanel panel_main = new JPanel();
    
    final JTable table = createTable();
    
    panel_main.add(table.getTableHeader());
    panel_main.setLayout(new BoxLayout(panel_main, BoxLayout.Y_AXIS));
    panel_main.add(table);

    return panel_main;
  }
  
  /**
   * @return The table that summarizes the status of each manuscript.
   */
  private JTable createTable() {
    final ManuscriptAccess manuscript_access = getDatabase().accessManuscript();
    final List<Manuscript> manuscripts = manuscript_access.getAllManuscripts();
//        manuscript_access.getViewableManuscripts(my_reviewer.getUser().getID(),
//                                                 my_reviewer.getConferenceID(),
//                                                 my_reviewer.getRoleID());
    final Object[][] data = new Object[manuscripts.size()][HEADERS.length];
    final ActionListener[][] als = new ActionListener[manuscripts.size()][HEADERS.length];
    final Iterator<Manuscript> itr = manuscripts.iterator();
    for (int i = 0; i < manuscripts.size(); i++) {
      //final Long m = itr.next();
      final Manuscript paper = itr.next();//MainPage.getDatabase().accessManuscript().getManuscript(m);
      data[i][COLUMN_AUTHOR_INDEX] = paper.getSubmittingAuthor();
      data[i][COLUMN_TITLE_INDEX] = paper.getTitle();
      als[i][COLUMN_TITLE_INDEX] = new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent the_e) {
          final DisplayPanel dsp = getDisplayStack();
          final ManuscriptSubmissionPanel msp = new ManuscriptSubmissionPanel(dsp, paper);
          dsp.addToStack(msp);
        }
      };
      data[i][COLUMN_ABSTRACT_INDEX] = paper.getAbstract();
      
      //get the reviewer's score, if any
      String score_str = STRING_NULL;
      final ReviewAccess database = MainPage.getDatabase().accessRev();
      final List<Long> review_ids = paper.getReviewIDs();
      for (Long id : review_ids) {
        final Review r = database.getReview(id);
        if (r.getReviewerID() == my_reviewer.getID()) {
          score_str = Integer.toString(r.getSummaryRating());
          data[i][COLUMN_BUTTON_INDEX] = "View Review";
          als[i][COLUMN_BUTTON_INDEX] = new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent the_e) {
              final DisplayPanel dsp = getDisplayStack();
              final ReviewSubmissionPanel msp =
                  new ReviewSubmissionPanel(dsp, my_reviewer, paper);
              dsp.addToStack(msp);
            }
          };
        }
      }
      data[i][COLUMN_SCORE_INDEX] = score_str;
      
      if (STRING_NULL.equals(score_str)) {
        data[i][COLUMN_BUTTON_INDEX] = "Submit Review";
        als[i][COLUMN_BUTTON_INDEX] = new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent the_e) {
            final DisplayPanel dsp = getDisplayStack();
            final ReviewSubmissionPanel msp =
                new ReviewSubmissionPanel(dsp, my_reviewer, paper);
            dsp.addToStack(msp);
          }
        };
      }
    }
    
    return new CustomTable(data, HEADERS, BOOLS, als);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String getPanelLabel() {
    return "Reviewer";
  }

}
